package com.shop.zkmall.app.service.a.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.zkmall.app.common.CommonRedisKey;
import com.shop.zkmall.app.domain.CereBuyerMerchant;
import com.shop.zkmall.app.domain.req.AddMch;
import com.shop.zkmall.app.domain.req.EditCereBuyerMerchant;
import com.shop.zkmall.app.domain.res.AddMachRecord;
import com.shop.zkmall.app.mapper.CereBuyerMerchantMapper;
import com.shop.zkmall.app.service.a.ICereBuyerMerchantService;
import com.shop.zkmall.app.service.buyer.CereBuyerReceiveService;
import com.shop.zkmall.app.utils.LoginHelper;
import com.shop.zkmall.app.utils.R;
import com.shop.zkmall.commons.cache.constants.CacheKeyConstants;
import com.shop.zkmall.commons.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家信息表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-11-25
 */
@Service
public class CereBuyerMerchantServiceImpl extends ServiceImpl<CereBuyerMerchantMapper, CereBuyerMerchant> implements ICereBuyerMerchantService {

    @Autowired
    private  CereBuyerReceiveService cereBuyerReceiveService;

    @Override
    public R<String> addMerchant(AddMch addMch) {
        //验证码校验
        String sendCodeKey = CacheKeyConstants.SEND_SMS_CODE_PREFIX + addMch.getPhone();
        ValidationUtil.checkValidateCode(sendCodeKey, addMch.getCode());
        Long userId = LoginHelper.getUserId();
        CereBuyerMerchant cereBuyerMerchant = new CereBuyerMerchant();
        cereBuyerMerchant.setUserId(userId);
        cereBuyerMerchant.setMerchantName(addMch.getMerchantName());
        cereBuyerMerchant.setName(addMch.getName());
        cereBuyerMerchant.setPhone(addMch.getPhone());
        cereBuyerMerchant.setArea(addMch.getArea());
        cereBuyerMerchant.setLegalCard(addMch.getLegalCard());
        cereBuyerMerchant.setAddress(addMch.getAddress());
        cereBuyerMerchant.setVatCertificate(addMch.getVatCertificate());
        cereBuyerMerchant.setBusinessLicense(addMch.getBusinessLicense());
        cereBuyerMerchant.setStatus(1);
        boolean save = this.save(cereBuyerMerchant);
        return R.ok();
    }

    @Override
    public R<List<AddMachRecord>> addMerchantRecord() {
        List<AddMachRecord> res = new ArrayList<>();
        Long userId = LoginHelper.getUserId();
        List<CereBuyerMerchant> list = lambdaQuery().eq(CereBuyerMerchant::getUserId, userId).list();
        if (CollUtil.isEmpty( list)){
            return R.ok(res);
        }
        for (CereBuyerMerchant cereBuyerMerchant : list) {
            AddMachRecord addMachRecord = new AddMachRecord();
            addMachRecord.setMerchantName(cereBuyerMerchant.getMerchantName());
            addMachRecord.setName(cereBuyerMerchant.getName());
            addMachRecord.setPhone(cereBuyerMerchant.getPhone());
            addMachRecord.setArea(cereBuyerMerchant.getArea());
            addMachRecord.setAddress(cereBuyerMerchant.getAddress());
            addMachRecord.setStatus(cereBuyerMerchant.getStatus());
            addMachRecord.setCause(cereBuyerMerchant.getCause());
            res.add(addMachRecord);
        }
        return R.ok(res);
    }

    @Override
    public R<?> editMerchant(EditCereBuyerMerchant editCereBuyerMerchant) {
        CereBuyerMerchant one = lambdaQuery().eq(CereBuyerMerchant::getId, editCereBuyerMerchant.getId()).one();
        if (one == null){
            return R.fail("商家不存在");
        }
        if (StrUtil.isNotBlank(editCereBuyerMerchant.getMerchantName())){
            one.setMerchantName(editCereBuyerMerchant.getMerchantName());
        }
        if (editCereBuyerMerchant.getClassifyId()!=null){
            one.setClassifyId(editCereBuyerMerchant.getClassifyId());
        }

        if (StrUtil.isNotBlank(editCereBuyerMerchant.getAvatar())){
            one.setAvatar(editCereBuyerMerchant.getAvatar());
        }

        if (StrUtil.isNotBlank(editCereBuyerMerchant.getPhone())){
            one.setPhone(editCereBuyerMerchant.getPhone());
        }
        if (StrUtil.isNotBlank(editCereBuyerMerchant.getDeliveryAddress())){
            one.setDeliveryAddress(editCereBuyerMerchant.getDeliveryAddress());
        }

        if (StrUtil.isNotBlank(editCereBuyerMerchant.getQrCode())){
            one.setQrCode(editCereBuyerMerchant.getQrCode());
        }

        if (StrUtil.isNotBlank(editCereBuyerMerchant.getLegalCard())){
            one.setLegalCard(editCereBuyerMerchant.getLegalCard());
        }

        if (StrUtil.isNotBlank(editCereBuyerMerchant.getVatCertificate())){
            one.setVatCertificate(editCereBuyerMerchant.getVatCertificate());
        }

        if (StrUtil.isNotBlank(editCereBuyerMerchant.getBusinessLicense())){
            one.setBusinessLicense(editCereBuyerMerchant.getBusinessLicense());
        }
        if (editCereBuyerMerchant.getBusinessStatus()!=null){
            one.setBusinessStatus(editCereBuyerMerchant.getBusinessStatus());
        }
        boolean update = this.updateById(one);
        return R.ok(update);
    }

    @Override
    public Long selShopId(Long buyerUserId) {
        CereBuyerMerchant one = lambdaQuery().eq(CereBuyerMerchant::getUserId, buyerUserId).one();
        if (one != null){
            return one.getId();
        }
        return 0L;
    }
}
